package com.unity3d.changelang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button).setOnClickListener(
                (view)->{
                    this.switchLang(view);
                }
        );
    }

    public void switchLang(View view) {
        String currentLanguage = Locale.getDefault().getLanguage();
        Log.d("log",currentLanguage);

        if (currentLanguage.equals("en")){
            Locale.setDefault(Locale.CHINESE);
            Configuration configuration = getBaseContext().getResources().getConfiguration();
            configuration.locale = Locale.CHINESE;
            getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
            recreate();
        }else {
            Locale.setDefault(Locale.ENGLISH);
            Configuration configuration = getBaseContext().getResources().getConfiguration();
            configuration.locale = Locale.ENGLISH;
            getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
            recreate();
        }
    }




}